package com.unicorn.util;

import com.unicorn.base.Routes;
import com.unicorn.entity.ServiceRegister;
import com.unicorn.util.interceptors.UnicornLogInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/28.
 */
@Configuration
public class UnicornWebMVCConfing extends WebMvcConfigurerAdapter {


    protected  static  Map<String,String> serviceRegisterMap=new HashMap<String,String>();


    private UnicornLogInterceptor logInterceptor;



    public UnicornWebMVCConfing() {
        logInterceptor=new UnicornLogInterceptor();
        registerAllService();
    }

    /**
     * 类扫描组件
     */
    private void registerAllService() {

        Routes routes=new Routes();
        Field[] services=Routes.class.getDeclaredFields();
        for(Field service:services){

            try {
                String serviceName=service.getName();
                String serviceValue=service.get(routes).toString();
                if(serviceName!=null && serviceValue!=null){
                    serviceRegisterMap.put(serviceName,serviceValue);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addWebRequestInterceptor(logInterceptor).addPathPatterns("/**");

    }


}
